package com.uh.checking.config;

import com.uh.checking.proc.JarCheckingUtils;
import com.uh.common.utils.FileType;
import com.uh.common.utils.FileUtils;

import java.io.File;
import java.util.List;

public class JarConfig {

    private String workingDir; // 工作路径

    private File workingDirFile = null; //  工作路径的 java.io.File对象

    private String srcJar; //源Jar文件

    private FileType srcJarType = null; ////源Jar类型，根据后缀判断。如：.tar.gz 是 TAR_GZ 类型

    private String targetJar; //处理后的目标Jar文件名，target文件会输出到源Jar文件所在的目录

    private String unzipDir; //解开后的目录名，会在和源Jar文件的同级目录下

    public String pathLibs ;

    public String pathClasses;

    private String beginStep; //开始步骤

    private String endStep; //结束步骤

    private SubJarConfig defaultSubJar; //设置 SubJarConfig 的缺省值

    private List<SubJarConfig> subJars;

    private List<SubJarShadeConfig> subJarShades;

    private String[] steps = null; // 分析 beginStep， endStep 后产生的step列表。

    public String getWorkingDir() {
        return workingDir;
    }

    public void setWorkingDir(String workingDir) {
        this.workingDir = workingDir;
    }

    public String getSrcJar() {
        return srcJar;
    }

    public void setSrcJar(String srcJar) {
        this.srcJar = srcJar;
    }

    public String getTargetJar() {
        return targetJar;
    }

    public void setTargetJar(String targetJar) {
        this.targetJar = targetJar;
    }

    public String getUnzipDir() {
        return unzipDir;
    }

    public void setUnzipDir(String unzipDir) {
        this.unzipDir = JarCheckingUtils.trimEndSeparator(unzipDir);
    }

    public String getBeginStep() {
        return beginStep;
    }

    public void setBeginStep(String beginStep) {
        this.beginStep = beginStep;
    }

    public String getEndStep() {
        return endStep;
    }

    public void setEndStep(String endStep) {
        this.endStep = endStep;
    }

    public SubJarConfig getDefaultSubJar() {
        return defaultSubJar;
    }

    public void setDefaultSubJar(SubJarConfig defaultSubJar) {
        this.defaultSubJar = defaultSubJar;
    }

    public List<SubJarConfig> getSubJars() {
        return subJars;
    }

    public void setSubJars(List<SubJarConfig> subJars) {
        this.subJars = subJars;
    }

    public List<SubJarShadeConfig> getSubJarShades() {
        return subJarShades;
    }

    public void setSubJarShades(List<SubJarShadeConfig> subJarShades) {
        this.subJarShades = subJarShades;
    }

    public String getPathLibs() {
        return pathLibs;
    }

    public void setPathLibs(String pathLibs) {
        this.pathLibs = JarCheckingUtils.trimEndSeparator(pathLibs);
    }

    public String getPathClasses() {
        return pathClasses;
    }

    public void setPathClasses(String pathClasses) {
        this.pathClasses = JarCheckingUtils.trimEndSeparator(pathClasses);
    }

    public String[] getSteps() {
        if(steps == null) {
            steps = JarCheckingUtils.trimSteps(JarCheckingConstants.MAIN_STEPS, this.beginStep, this.endStep);
        }
        return steps;
    }

    public FileType getSrcJarType() {
        if(srcJarType == null) {
            srcJarType = FileUtils.getFileType(srcJar);
        }

        return srcJarType;
    }

    public File getWorkingDirFile() {
        if(workingDir != null && workingDirFile == null) {
            workingDirFile = new File(workingDir);
            System.out.println("Config workingDir=" + workingDirFile.getAbsolutePath());
            if(! workingDirFile.exists()) {
                throw new ConfigException("Config workingDir '" + workingDirFile.getAbsolutePath() + "' is not exists!");
            }
            if(! workingDirFile.isDirectory()) {
                throw new ConfigException("Config workingDir '" + workingDirFile.getAbsolutePath() + "' is not directory!");
            }

            workingDirFile.getAbsolutePath();
        }

        return workingDirFile;
    }

    /**
     * 把缺省配置合并到 subJars 中的每个成员上
     */
    public void mergeDefaultSubJars() {
        if(defaultSubJar != null) {
            if (subJars != null && subJars.size() > 0) {
                for (SubJarConfig subJar : subJars) {
                    if (defaultSubJar.getBeginStep() != null && subJar.getBeginStep() == null) {
                        subJar.setBeginStep(defaultSubJar.getBeginStep());
                    }
                    if (defaultSubJar.getEndStep() != null && subJar.getEndStep() == null) {
                        subJar.setEndStep(defaultSubJar.getEndStep());
                    }
                    if (defaultSubJar.getDeleteFiles() != null && subJar.getDeleteFiles() == null) {
                        subJar.setDeleteFiles(defaultSubJar.getDeleteFiles());
                    }

                    if (defaultSubJar.getManifestIncludeKeys() != null && subJar.getManifestIncludeKeys() == null) {
                        subJar.setManifestIncludeKeys(defaultSubJar.getManifestIncludeKeys());
                    }

                }
            }
        }
    }

    @Override
    public String toString() {
        return "JarConfig{" +
                "srcJar='" + srcJar + '\'' +
                ", targetJar='" + targetJar + '\'' +
                ", unzipDir='" + unzipDir + '\'' +
                ", beginStep='" + beginStep + '\'' +
                ", endStep='" + endStep + '\'' +
                ", \ndefaultSubJar=" + defaultSubJar +
                ", \nsubJars=[" +  ((subJars == null) ? "" : subJars.size()) + "]" +
                ", \nsubJarShards=[" + ((subJarShades == null) ? "" : subJarShades.size()) + "]" +
                '}';
    }
}
